<?php

/*
    Dill is a tool for creating websites with PHP.
    Copyright (C) 2014  Jannik Haberbosch

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once( "WebsiteProjectFileElement.php" );

class WebsiteProjectTemplate extends WebsiteProjectFileElement
{
	static protected $directory;
	static protected $abspath_directory;
	private $function_container_names = array();
	private $page_container_names = array();

	function __construct( $name )
	{
		parent::__construct( $name );
		$this->update_container_names();
	}


	public function update_container_names()
	{
		$content = $this->get_content();
			
		$this->function_container_names = $this->extract_function_containers( $content );
		$this->page_container_names = $this->extract_page_containers( $content );
	}


	private function extract_page_containers( $contents )
	{
		/* This function extracts page containers from a string '$contents'.
		On success, an array with the page containers (strings) is returned.
		In case of an error, 'FALSE' is returned.

		Parameters		$contents
						string
						A string that contains the contents of a template.

		Return values	Array or Boolean:
						An array is returned on success.  Otherwise a boolean value
						of FALSE is returned.
	
		*/
		$arr = array();

		$result = preg_match_all( REGEX_PAGE_CONTAINER, $contents, $matches );
		if( $result === 0 ) return $arr;
		if( $result === FALSE ) return $arr;

		$matches = $matches[0];
		$matches = array_filter( $matches, function( $element )
		{
			if( strpbrk( $element, "()" ) == FALSE )
			{
				return TRUE;
			}
			return FALSE;
		});			
		return $matches;
	}


	private function extract_function_containers( $contents )
	{
		/* This function extracts function containers that are contained in a
		string. 
		On success, these function containers (strings) are returned, contained in
		an array.  FALSE is returned in case of an error.

		Parameters		$contents
						string
						A string that contains the contents of a template.

		Return values	Array or Boolean.
						An array is returned on success, a boolean 'FALSE' on failure.

		*/
		$matches = array();
		if( preg_match_all( REGEX_FUNCTION_CONTAINER, $contents, $matches ) )
		{
			if( count( $matches[0] ) == 0 )
			{
				return array();
			}
			else
			{
				return $matches[0];
			}
		}
		return $matches;
	}


	public function get( $property )
	{
		return $this->$property;
	}
}

?>
